package com.chdwmp.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.chdwmp.product.bean.PageResult;
import com.chdwmp.product.bean.backend.AppUserSystemInfo;
import com.chdwmp.product.bean.params.AppUserSystemSearch;
import com.chdwmp.product.constant.CommonConstants;
import com.chdwmp.product.constant.Status;
import com.chdwmp.product.exception.CommonException;
import com.chdwmp.product.mapper.BackendMapper;
import com.chdwmp.product.service.BackendService;
import com.chdwmp.product.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Auther: LLL
 * @Date: 2021/3/20 10:27
 * @Description:
 */
@Service
public class BackendServiceImpl implements BackendService {

    @Autowired
    BackendMapper backendMapper;

    @Override
    public PageResult getBackendAppUserSystemInfo(AppUserSystemSearch appUserSystemSearch) {

        int rows = appUserSystemSearch.getPageSize();
        if (rows == 0) {
            rows = CommonConstants.PAGE_SIZE;
        }
        int start_rows = appUserSystemSearch.getPage() * rows;
        appUserSystemSearch.setPage(start_rows);
        appUserSystemSearch.setPageSize(rows);
        List<AppUserSystemInfo> list = null;
        int total_count  = backendMapper.queryTotalCount(appUserSystemSearch);
        if(total_count > 0){
            list = backendMapper.queryBackendAppUserSystemList(appUserSystemSearch);
            for (AppUserSystemInfo appUserSystemInfo : list) {
                String productIds = appUserSystemInfo.getProductIds();
                if(StrUtil.isNotEmpty(productIds) && productIds.contains(",")){
                    String[] str_array = productIds.split(",");
                    List<String> names  = new ArrayList<>();
                    if (null != str_array && str_array.length > 0) {
                        for (String s : str_array) {
                            String productName = backendMapper.queryProductNameById(s);
                            names.add(productName);
                        }
                    }
                    appUserSystemInfo.setSecondClassCustomerName(StringUtil.formatListToString6(names));
                }else{
                    String productName = backendMapper.queryProductNameById(appUserSystemInfo.getProductIds());
                    appUserSystemInfo.setSecondClassCustomerName(productName);
                }
            }
        }else{
            list =  new ArrayList<>();
        }
        boolean isHasMore = true;
        if(rows == 0){
            isHasMore = total_count > (appUserSystemSearch.getPage() + 1) * CommonConstants.PAGE_SIZE;
        }else {
            isHasMore = total_count > (appUserSystemSearch.getPage() + 1) * rows;
        }
        if (total_count == 0){
            return new PageResult<>(total_count,isHasMore, Collections.emptyList());
        }
        return new PageResult<>(total_count,isHasMore,list);
    }

    @Override
    @Transactional
    public void setBlackList(String id) {
        if(ObjectUtil.isNull(id))
            throw new CommonException(Status.MISS_REQUIRED_PARAMETER);
        String status  = backendMapper.queryStatus(id);
        if("1".equals(status)){
            backendMapper.setBlackListById(id,0);
        }else {
            backendMapper.setBlackListById(id,1);
        }
    }
}
